package cn.ming.wfchat.app.me.pop;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;
import cn.ming.wfchat.R;
import cn.ming.wfchat.kit.dialog.CenterDialog;
import cn.ming.wfchat.app.me.activity.MoneyFlowingWaterActivity;
import cn.ming.wfchat.databinding.PopupWindowBrushToChooseviewBinding;

/**
 * 刷选弹框
 */
public class BrushPopupWindow extends CenterDialog {
    private PopupWindowBrushToChooseviewBinding binding;
    private MoneyFlowingWaterActivity.logEnum logEnum;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.DialogStyle);
        ViewDataBinding binding = getLayoutBind();
        View view = binding.getRoot();
        dialog.setCancelable(isCancele());
        dialog.setCanceledOnTouchOutside(isCancele());
        dialog.setContentView(view);
        initView(binding);
        initWindowParams(dialog);
        return dialog;
    }

    public static BrushPopupWindow newInstance() {
        BrushPopupWindow dialog = new BrushPopupWindow();
        Bundle bundle = new Bundle();
        dialog.setArguments(bundle);
        return dialog;
    }

    // 设置Dialog属性
    protected void initWindowParams(Dialog dialog) {
        Window win = dialog.getWindow();
        if (win != null) {
            WindowManager.LayoutParams lp = win.getAttributes();
            //win.setWindowAnimations(R.style.DialogTheme_BottomAnimation);
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.gravity = Gravity.TOP;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            win.setDimAmount(0f);//去除蒙层
            win.setAttributes(lp);
        }
    }

    private OnPopupClickListener onPopupClickListener;

    public BrushPopupWindow setOnPraiseOrCommentClickListener(OnPopupClickListener onPopupClickListener) {
        this.onPopupClickListener = onPopupClickListener;
        return this;
    }


    @Override
    public int getLayoutId() {
        return R.layout.popup_window_brush_to_chooseview;
    }

    @Override
    public void initView(ViewDataBinding dataBinding) {
        binding = (PopupWindowBrushToChooseviewBinding) dataBinding;

        binding.radioGroup.setOnCheckedChangeListener(null);
        switch (logEnum) {
            case ALL:
                //binding.radioGroup.check(R.id.rb_1);
                binding.rb1.setChecked(true);
                break;
            case RECHARGE:
                binding.radioGroup.check(R.id.rb_2);
                break;
            case WITHDRAW:
                binding.radioGroup.check(R.id.rb_3);
                break;
            case TRANSFER:
                binding.radioGroup.check(R.id.rb_4);
                break;
            case MOMENTS:
                binding.radioGroup.check(R.id.rb_5);
                break;
            case COMMISSION:
                binding.radioGroup.check(R.id.rb_6);
                break;

        }
        binding.radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener());

    }

    public interface OnPopupClickListener {
        void onPopup(MoneyFlowingWaterActivity.logEnum logEnum);
    }

    public void show(Object object, MoneyFlowingWaterActivity.logEnum logEnum) {
        this.logEnum = logEnum;
        super.show(object, this.toString());
    }

    class OnCheckedChangeListener implements MyRadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(MyRadioGroup group, int checkedId) {
            dismissAllowingStateLoss();
            switch (checkedId) {
                case R.id.rb_1:
                    onPopupClickListener.onPopup(MoneyFlowingWaterActivity.logEnum.ALL);
                    break;
                case R.id.rb_2:
                    onPopupClickListener.onPopup(MoneyFlowingWaterActivity.logEnum.RECHARGE);
                    break;
                case R.id.rb_3:
                    onPopupClickListener.onPopup(MoneyFlowingWaterActivity.logEnum.WITHDRAW);
                    break;
                case R.id.rb_4:
                    onPopupClickListener.onPopup(MoneyFlowingWaterActivity.logEnum.TRANSFER);
                    break;
                case R.id.rb_5:
                    onPopupClickListener.onPopup(MoneyFlowingWaterActivity.logEnum.MOMENTS);
                    break;
                case R.id.rb_6:
                    onPopupClickListener.onPopup(MoneyFlowingWaterActivity.logEnum.COMMISSION);
                    break;
                case R.id.rb_7:
                    onPopupClickListener.onPopup(MoneyFlowingWaterActivity.logEnum.REDPACKET);
                    break;
            }
        }
    }
}
